﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_QuangCao : System.Web.UI.Page
{
    QuangCao qc = new QuangCao();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["user"] != null)
            {
                MultiView1.SetActiveView(View1);
                GridView1.DataSource = qc.DLQuangCao();
                GridView1.DataBind();
            }
            else
                Response.Redirect("DangNhap.aspx");
        }
    }
    protected void Btn_Tim_Click(object sender, EventArgs e)
    {
        string gia = TextBox1.Text;
        GridView1.DataSource = qc.TimQC(TextBox9.Text, TextBox1.Text, TextBox3.Text);
        GridView1.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = qc.DLQuangCao();
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = qc.DLQuangCao();
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        TextBox txtcty = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
        string tencty = txtcty.Text;

        FileUpload fup = (FileUpload)GridView1.Rows[e.RowIndex].Cells[2].FindControl("FileUpload1");
        string hanh = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        if (fup.HasFile)
        {
            hanh = Path.GetFileName(fup.PostedFile.FileName);
            fup.PostedFile.SaveAs(Server.MapPath("../Pictures/CD/") + hanh);
        }

        TextBox txtgia = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
        string gia = txtgia.Text;

        TextBox txtutien = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
        string utien = txtutien.Text;

        TextBox txtul = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
        string url = txtul.Text;

        TextBox txngaydang = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
        string ndang = txngaydang.Text;

        TextBox txtngayhuy = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0];
        string nhuy = txtngayhuy.Text;

        qc.SuaQC(tencty, hanh,float.Parse( gia),int.Parse( utien),url, ndang, nhuy);

        GridView1.EditIndex = -1;
        GridView1.DataSource = qc.DLQuangCao();
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = qc.DLQuangCao();
        GridView1.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View2);
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        string gia = TextBox5.Text;
        string uutien = TextBox6.Text;
          string hanh = Path.GetFileName(File2.PostedFile.FileName);
          File2.PostedFile.SaveAs(Server.MapPath("../Pictures/QuangCao/") + hanh);

        GridView1.DataSource = qc.ThemQC(TextBox4.Text,hanh,float.Parse(gia),int.Parse(uutien),TextBox10.Text, TextBox7.Text,TextBox8.Text);
        GridView1.DataBind();
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
        MultiView1.SetActiveView(View1);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                if (chb.Checked == true)
                {
                    string ma =GridView1.Rows[i].Cells[1].Text;
                    qc.XoaQC(ma);
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;

        }
        GridView1.DataSource = qc.DLQuangCao();
        GridView1.DataBind();
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
}
